package cz.jm.web.manhunter.server.authentication;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import cz.jm.web.manhunter.shared.User;

@SuppressWarnings("serial")
public class Account implements Serializable {
	public static final long INVALID_ID = -1;
	private long id = INVALID_ID;
	private String username; 
	private String mail;
	
	private Date registrationDate;
	private Date verificationDate;
	private Date lastAccessDate;
	private Date deleteDate;
	
	private List<String> phoneNumbers = new ArrayList<String>();
	
	
	public Account() { }
	
	public Account(User user) { 
		initFromUser(user);
		setRegistrationDate(new Date());
		setLastAccessDate(new Date());
	}

	public long getId() {
	    return id;
    }

	public void setId(long id) {
	    this.id = id;
    }

	public boolean isValid() { return id != INVALID_ID; }


	public String getMail() {
	    return mail;
    }


	public void setMail(String mail) {
	    this.mail = mail;
    }


	public List<String> getPhoneNumbers() {
	    return phoneNumbers;
    }

	public void setPhoneNumbers(List<String> phoneNumbers) {
		// Tato trieda je prenasana skrz GWT RPC a sucasne perzistovana pomocou Hibernate.
		// Hibernarte vytvara vlastnu implementaciu List tzv PersistentList a ten nie je prenesitelny skrz
		// GWT RPC.
		if (phoneNumbers instanceof ArrayList<?> || phoneNumbers instanceof LinkedList<?>) {
			this.phoneNumbers = phoneNumbers;
		}
		else {
			this.phoneNumbers = new ArrayList<String>(phoneNumbers);
		}
    }

	public String getUsername() {
    	return username;
    }

	public void setUsername(String username) {
    	this.username = username;
    }
	
	public void initFromUser(User user) {
		id = user.getId();
		username = user.getUsername();
		mail = user.getMail();
		phoneNumbers = user.getPhoneNumbers();
	}
	
	public void initUser(User user) {
		user.setId(id);
		user.setUsername(username);
		user.setMail(mail);
		// Account ma tel. cisla vlozene v specialnej lazy implamentacii
		// List-u, ktora nejde serializovat a preto treba vytvorit novy zoznam.
		user.setPhoneNumbers(new ArrayList<String>(phoneNumbers));		
	}

	public Date getRegistrationDate() {
    	return registrationDate;
    }

	public void setRegistrationDate(Date registrationDate) {
    	this.registrationDate = registrationDate;
    }

	public Date getVerificationDate() {
    	return verificationDate;
    }

	public void setVerificationDate(Date verificationDate) {
    	this.verificationDate = verificationDate;
    }

	public Date getLastAccessDate() {
    	return lastAccessDate;
    }

	public void setLastAccessDate(Date lastAccessDate) {
    	this.lastAccessDate = lastAccessDate;
    }

	public Date getDeleteDate() {
    	return deleteDate;
    }

	public void setDeleteDate(Date deleteDate) {
    	this.deleteDate = deleteDate;
    }
}
